﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kalficom
{
    public partial class frmContract : Form
    {
        public frmContract()
        {
            InitializeComponent();
            conf.Icon(this);
        }

        Libraies.db db = new Libraies.db();
        Query.qry_contract query = new Query.qry_contract();

        private void frmContract_Load(object sender, EventArgs e)
        {
            db.FillDataSet(query.get_list, tblContract);
            this.WindowState = FormWindowState.Maximized;
        }

        private void btnMainMenu_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            using (new WaitCursor())
            {
                (new Forms.frmContract_CreateModify()).ShowDialog();
                db.FillDataSet(query.get_list, tblContract);
            }
           
        }

        private void dgContract_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            Forms.frmContract_CreateModify frm = new Forms.frmContract_CreateModify();
            frm.Tag = dgContract.Rows[e.RowIndex].Cells["cont_id"].Value;

            frm.ShowDialog();
            
                // Read user record from database
                db.FillDataSet(query.get_list, tblContract, false);
           
        }

        private void dgContract_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == dgContract.Columns["colDelete"].Index)
            {
                Libraies.msg msg = new Libraies.msg();
                if (MessageBox.Show(msg.deleteMsg, msg.captionMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // Delete zone from database.
                    db.ExecuteNonQuery(string.Format(query.del_cont_taken_analyze, dgContract.CurrentRow.Cells["cont_id"].Value));
                    db.ExecuteNonQuery(string.Format(query.del_cont_taken, dgContract.CurrentRow.Cells["cont_id"].Value));
                    db.ExecuteNonQuery(string.Format(query.del_cont_client_taken, dgContract.CurrentRow.Cells["cont_id"].Value));
                    db.ExecuteNonQuery(string.Format(query.deleted, dgContract.CurrentRow.Cells["cont_id"].Value));

                    // Delete zone record from list.
                    tblContract.Select(string.Format("cont_id={0}", dgContract.CurrentRow.Cells["cont_id"].Value))[0].Delete();
                }
            }
        }
    }
}
